package org.cn.springcloudvv.cxf.controller;

import org.cn.springcloudvv.base.Result;
import org.cn.springcloudvv.cxf.client.User;
import org.cn.springcloudvv.cxf.client.WebServiceUserImpl;
import org.cn.springcloudvv.cxf.client.WebServiceUserImplService;
import org.springframework.web.bind.annotation.*;


import javax.xml.namespace.QName;
import java.net.URL;

@RestController
public class UserInfoClientController {

    private static final QName SERVICE_NAME = new QName("http://impl.service.cxf.springcloudvv.cn.org/", "WebServiceUserImplService");


    @PostMapping("/client/getUser")
    public Result getUser(User user) throws Exception {

        URL wsdlURL = WebServiceUserImplService.WSDL_LOCATION;

        WebServiceUserImplService ss = new WebServiceUserImplService(wsdlURL, SERVICE_NAME);
        WebServiceUserImpl port = ss.getWebServiceUserImplPort();
        user.setUserId(user.getUserId()+"-"+System.currentTimeMillis());

        User userTemp = port.getUser(user);
        Result result = Result.of(200,"");
        result.setData(userTemp);
        result.setMessage("webservice客户端，经过webservice-proxy，调用微服务rest-api，并返回api接口执行结果");
        return result;

    }
    @PostMapping("/client/addUser")
    public Result addUser(User user) throws Exception {

        URL wsdlURL = WebServiceUserImplService.WSDL_LOCATION;

        WebServiceUserImplService ss = new WebServiceUserImplService(wsdlURL, SERVICE_NAME);
        WebServiceUserImpl port = ss.getWebServiceUserImplPort();
        user.setUserId(user.getUserId()+"-"+System.currentTimeMillis());

        User userTemp = port.addUser(user);
        Result result = Result.of(200,"");
        result.setData(userTemp);
        result.setMessage("webservice客户端，经过webservice-proxy，调用微服务rest-api，并返回api接口执行结果");
        return result;

    }


    /**
     * description: 获取用户json作为前面方法的入参
     */
    @RequestMapping(value = "/client/getUserJson" ,method = RequestMethod.POST)
    public User getUser(@RequestParam("id") String id) throws Exception {

        User user = new User();
        user.setUserId(id);
        user.setUserName("testUsername");
        user.setAge(18);
        user.setPhone(""+System.currentTimeMillis());
        return user;
    }

}
